﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GLYFrom.DDGL
{
    public partial class DD_SeleDele : Form
    {
        public DD_SeleDele()
        {
            InitializeComponent();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //订单ID
            int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Col_DDId"].Value);
            //获取选中行的订单状态
            DataTable Table = DBHelper.select(string.Format("select * from T_DingDan where DDId='{0}'", id));
            int ZT = Convert.ToInt32(Table.Rows[0]["DDZT"]);
            //派送中
            int ps = Convert.ToInt16(MeiJu.PS);
            //已完成
            int wc = Convert.ToInt16(MeiJu.WC);
            if (ZT == ps || ZT == wc)
            {
                MessageBox.Show("派送失败,该订单以派送或以完成...");
                return;
            }
            int sr = DBHelper.ZSG(string.Format("update T_DingDan set DDZT='{0}' where DDId='{1}'", ps, id));//修改订单状态
            if (sr > 0)
            {
                MessageBox.Show("派送成功！");
                DataTable table = DBHelper.select("select * from T_DingDan where DDZT!=4 and DDZT!=2");
                this.dataGridView1.DataSource = null;
                this.dataGridView1.DataSource = table;
            }
            else
            {
                MessageBox.Show("派送失败！");
            }
            foreach (DataGridViewRow ro in this.dataGridView1.Rows)
            {
                int zt = Convert.ToInt16(ro.Cells["Col_ZT"].Value);
                if (zt == (int)MeiJu.XD)
                {
                    ro.Cells["Col_ZT"].Value = "以下单";
                }
                else if (zt == (int)MeiJu.PS)
                {
                    ro.Cells["Col_ZT"].Value = "派送中";
                }
                else
                {
                    ro.Cells["Col_ZT"].Value = "已完成";
                }
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void DD_SeleDele_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            //身份为用户
            if (Convert.ToInt32(GeRenXX.ShenFen)==Convert.ToInt32(MeiJu.WC))
            {
                int id=GeRenXX.Id;
                DataTable table = DBHelper.select(string.Format("select * from T_DingDan where YHId='{0}' and DDZT!=4", id));
                this.dataGridView1.DataSource = table;
                this.删除ToolStripMenuItem.Visible = false;
                this.刷新ToolStripMenuItem1.Visible = false;
                this.label1.Text = "请    输    入    查    询    信    息   :";
                this.text_Id.Visible = false;
            }
            //管理员
            else
            {

                this.隐藏已完成订单ToolStripMenuItem.Visible = false;
                DataTable table = DBHelper.select("select * from T_DingDan where DDZT!=4 and DDZT!=2");
                this.dataGridView1.DataSource = table;
            }
            foreach (DataGridViewRow ro in this.dataGridView1.Rows)
            {
                int ZT = Convert.ToInt16(ro.Cells["Col_ZT"].Value);
                if (ZT == (int)MeiJu.XD)
                {
                    ro.Cells["Col_ZT"].Value = "以下单";
                }
                else if (ZT == (int)MeiJu.PS)
                {
                    ro.Cells["Col_ZT"].Value = "派送中";
                }
                else
                {
                    ro.Cells["Col_ZT"].Value = "已完成";
                }
            }
         }

        private void button1_Click(object sender, EventArgs e)
        {
            string YHId = this.text_Id.Text.Trim();
            string DDH = this.text_DDH.Text.Trim();
            string Name = this.text_Name.Text.Trim();
            string Time = this.text_Time.Text.Trim();
            string JiaGe = this.text_JiaGe.Text.Trim();
            string sql = "select * from T_DingDan where 1=1";
            if (YHId.Length>0)
            {
                sql += string.Format("and YHId like '%{0}%'",YHId);
            }
            if (DDH.Length > 0)
            {
                sql += string.Format("and DDH like '%{0}%'", DDH);
            }
            if (Name.Length > 0)
            {
                sql += string.Format("and CPName like '%{0}%'", Name);
            }
            if (Time.Length > 0)
            {
                sql += string.Format("and time like '%{0}%'", Time);
            }
            if (JiaGe.Length > 0)
            {
                sql += string.Format("and JiaGe like '%{0}%'", JiaGe);
            }
            int sf = GeRenXX.ShenFen;
            if (sf==2)
            {
                sql += string.Format("and YHId='{0}' ",YHId);
            }
            DataTable table = DBHelper.select(sql);
            this.dataGridView1.DataSource = null;
            this.dataGridView1.DataSource = table;

            foreach (DataGridViewRow ro in this.dataGridView1.Rows)
            {
                int ZT = Convert.ToInt16(ro.Cells["Col_ZT"].Value);
                if (ZT == (int)MeiJu.XD)
                {
                    ro.Cells["Col_ZT"].Value = "以下单";
                }
                else if (ZT == (int)MeiJu.PS)
                {
                    ro.Cells["Col_ZT"].Value = "派送中";
                }
                else
                {
                    ro.Cells["Col_ZT"].Value = "已完成";
                }
            }
        }

        private void 刷新ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //订单ID
            int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Col_DDId"].Value);
            //获取选中行的订单状态
            DataTable Table = DBHelper.select(string.Format("select * from T_DingDan where DDId='{0}'",id));
            int ZT = Convert.ToInt32(Table.Rows[0]["DDZT"]);
            //已完成
            int wc = Convert.ToInt16(MeiJu.WC);
            if (ZT == wc)
            {
                MessageBox.Show("操作失败,该订单以完成...");
                return;
            }
            int sr = DBHelper.ZSG(string.Format("update T_DingDan set DDZT='{0}' where DDId='{1}'", wc, id));//修改订单状态
            if (sr > 0)
            {
                MessageBox.Show("订单已完成！");
                DataTable table = DBHelper.select("select * from T_DingDan where DDZT!=4 and DDZT!=2");
                this.dataGridView1.DataSource = null;
                this.dataGridView1.DataSource = table;
            }
            else
            {
                MessageBox.Show("操作失败！");
            }
            foreach (DataGridViewRow ro in this.dataGridView1.Rows)
            {
                int zt = Convert.ToInt16(ro.Cells["Col_ZT"].Value);
                if (zt == (int)MeiJu.XD)
                {
                    ro.Cells["Col_ZT"].Value = "以下单";
                }
                else if (zt == (int)MeiJu.PS)
                {
                    ro.Cells["Col_ZT"].Value = "派送中";
                }
                else
                {
                    ro.Cells["Col_ZT"].Value = "已完成";
                }
            }
        }

        private void 取消ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("你确定要取消订单吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                //订单Id
                int id = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells["Col_DDId"].Value);
                //用户Id
                int ID=GeRenXX.Id;
                int sr = DBHelper.ZSG(string.Format("delete from T_DingDan  where DDId='{0}'", id));
                if (sr > 0)
                {
                    MessageBox.Show("订单取消成功！");
                    //身份为用户
                    if (Convert.ToInt32(GeRenXX.ShenFen) == Convert.ToInt32(MeiJu.WC))
                    {
                        DataTable table=DBHelper.select("select * from T_YongHu");
                        int qq=Convert.ToInt32(table.Rows[0]["YuE"]);
                        int Money = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells["Col_JiaGe"].Value);
                        Money+=qq;
                        int xg = DBHelper.ZSG(string.Format("update T_YongHu set YuE='{0}' where YhId='{1}'",Money,ID));
                        DataTable Table = DBHelper.select(string.Format("select * from T_DingDan where YHId='{0}' and DDZT!=4", ID));
                        this.dataGridView1.DataSource = null;
                        this.dataGridView1.DataSource = Table;
                    }
                    //管理员
                    else
                    {
                        DataTable table = DBHelper.select("select * from T_DingDan");
                        this.dataGridView1.DataSource = null;
                        this.dataGridView1.DataSource = table;
                    }
                    foreach (DataGridViewRow ro in this.dataGridView1.Rows)
                    {
                        int ZT = Convert.ToInt16(ro.Cells["Col_ZT"].Value);
                        if (ZT == (int)MeiJu.XD)
                        {
                            ro.Cells["Col_ZT"].Value = "以下单";
                        }
                        else if (ZT == (int)MeiJu.PS)
                        {
                            ro.Cells["Col_ZT"].Value = "派送中";
                        }
                        else
                        {
                            ro.Cells["Col_ZT"].Value = "已完成";
                        }
                    }
                }
                else
                {
                    MessageBox.Show("订单取消失败！");
                }
            }
        }

        private void 刷新ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            //身份为用户
            if (Convert.ToInt32(GeRenXX.ShenFen) == Convert.ToInt32(MeiJu.WC))
            {
                int id = GeRenXX.Id;
                DataTable table = DBHelper.select(string.Format("select * from T_DingDan where YHId='{0}' and DDZT!=4", id));
                this.dataGridView1.DataSource = null;
                this.dataGridView1.DataSource = table;
            }
            //管理员
            else
            {
                DataTable table = DBHelper.select("select * from T_DingDan");
                this.dataGridView1.DataSource = null;
                this.dataGridView1.DataSource = table;
                
            }
            foreach (DataGridViewRow ro in this.dataGridView1.Rows)
            {
                int ZT = Convert.ToInt16(ro.Cells["Col_ZT"].Value);
                if (ZT == (int)MeiJu.XD)
                {
                    ro.Cells["Col_ZT"].Value = "以下单";
                }
                else if (ZT == (int)MeiJu.PS)
                {
                    ro.Cells["Col_ZT"].Value = "派送中";
                }
                else
                {
                    ro.Cells["Col_ZT"].Value = "已完成";
                }
            }

           }

        private void 隐藏已完成订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int id = GeRenXX.Id;
            DataTable table = DBHelper.select(string.Format("select * from T_DingDan where YHId='{0}' and DDZT!=4 and DDZT!=2", id));
            this.dataGridView1.DataSource = null;
            this.dataGridView1.DataSource = table;
            foreach (DataGridViewRow ro in this.dataGridView1.Rows)
            {
                int ZT = Convert.ToInt16(ro.Cells["Col_ZT"].Value);
                if (ZT == (int)MeiJu.XD)
                {
                    ro.Cells["Col_ZT"].Value = "以下单";
                }
                else if (ZT == (int)MeiJu.PS)
                {
                    ro.Cells["Col_ZT"].Value = "派送中";
                }
                else
                {
                    ro.Cells["Col_ZT"].Value = "已完成";
                }
            }
        }
    }
}
